<script setup>
import { useThemeStore } from './stores/themeStore'
import { onMounted } from 'vue'

const themeStore = useThemeStore()

onMounted(() => {
  // 检查系统主题偏好
  if (
    window.matchMedia &&
    window.matchMedia('(prefers-color-scheme: dark)').matches
  ) {
    themeStore.currentTheme = 'dark'
    document.documentElement.setAttribute('data-theme', 'dark')
  }
})
</script>

<template>
  <div class="app">
    <nav class="no-select">
      <div class="nav-content">
        <h1 class="logo">📝 记事本</h1>
        <div class="nav-links">
          <router-link to="/home">首页</router-link>
          <router-link to="/notes">笔记列表</router-link>
        </div>
      </div>
    </nav>

    <main>
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </main>

    <!-- 主题切换按钮 -->
    <button
      class="theme-toggle"
      @click="themeStore.toggleTheme"
      :title="
        themeStore.currentTheme === 'light'
          ? '切换到深色模式'
          : '切换到浅色模式'
      "
    >
      {{ themeStore.currentTheme === 'light' ? '🌙' : '☀️' }}
    </button>
  </div>
</template>

<style>
@import './assets/main.css';

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

nav {
  background-color: var(--nav-background);
  box-shadow: 0 2px 10px var(--shadow-color);
  position: fixed;
  width: 100%;
  top: 0;
  z-index: 100;
  transition: background-color 0.3s ease;
}

.nav-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav-items {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.logo {
  color: var(--primary-color);
  font-size: 1.5rem;
}

.nav-links {
  display: flex;
  gap: 1.5rem;
}

.nav-links a {
  text-decoration: none;
  color: var(--text-color);
  font-weight: 500;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.nav-links a:hover {
  background-color: var(--background-color);
}

.nav-links a.router-link-active {
  color: var(--primary-color);
  background-color: color-mix(in srgb, var(--primary-color) 10%, transparent);
}

.theme-toggle {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  background-color: var(--card-background);
  color: var(--text-color);
  border: 1px solid var(--border-color);
  box-shadow: 0 4px 12px var(--shadow-color);
  z-index: 1000;
  cursor: pointer;
  transition: all 0.3s ease;
}

.theme-toggle:hover {
  transform: scale(1.1) rotate(360deg);
  box-shadow: 0 6px 16px var(--shadow-color);
}

main {
  flex: 1;
  max-width: 1200px;
  width: 100%;
  margin: 80px auto 0;
  padding: 2rem;
  overflow-y: auto;
  height: calc(100vh - 80px);
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 适配移动设备 */
@media (max-width: 768px) {
  .nav-content {
    padding: 1rem;
  }

  .nav-links {
    gap: 0.5rem;
  }

  .nav-links a {
    padding: 0.5rem;
  }

  main {
    padding: 1rem;
  }

  .theme-toggle {
    bottom: 1rem;
    right: 1rem;
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
  }
}
</style>
